package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.WorkflowTaskMapper;
import com.zhentao.pojo.WorkflowHistory;
import com.zhentao.pojo.WorkflowNode;
import com.zhentao.pojo.WorkflowTask;
import com.zhentao.service.WorkflowHistoryService;
import com.zhentao.service.WorkflowNodeService;
import com.zhentao.service.WorkflowTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 流程任务Service实现类
 */
@Service
public class WorkflowTaskServiceImpl extends ServiceImpl<WorkflowTaskMapper, WorkflowTask> implements WorkflowTaskService {
    
    @Autowired
    private WorkflowNodeService workflowNodeService;
    
    @Autowired
    private WorkflowHistoryService workflowHistoryService;
    
    @Override
    public List<WorkflowTask> getTodoTasksByAssignee(Long assigneeId) {
        return baseMapper.selectTodoTasksByAssignee(assigneeId);
    }
    
    @Override
    public List<WorkflowTask> getDoneTasksByAssignee(Long assigneeId) {
        return baseMapper.selectDoneTasksByAssignee(assigneeId);
    }
    
    @Override
    public List<WorkflowTask> getTasksByBill(String billType, Long billId) {
        return baseMapper.selectByBill(billType, billId);
    }
    
    @Override
    @Transactional
    public boolean startWorkflow(Long processId, String billType, Long billId, Long assigneeId) {
        // 获取流程的第一个节点
        List<WorkflowNode> nodes = workflowNodeService.getNodesByProcessId(processId);
        if (nodes.isEmpty()) {
            return false;
        }
        
        WorkflowNode firstNode = nodes.get(0);
        
        // 创建第一个任务
        WorkflowTask task = new WorkflowTask();
        task.setProcessId(processId);
        task.setNodeId(firstNode.getId());
        task.setBillType(billType);
        task.setBillId(billId);
        task.setStatus(0); // 待办
        task.setAssigneeId(firstNode.getApproverId());
        task.setStartTime(new Date());
        
        boolean result = save(task);
        
        // 记录历史
        if (result) {
            WorkflowHistory history = new WorkflowHistory();
            history.setTaskId(task.getId());
            history.setAction("启动流程");
            history.setUserId(assigneeId);
            history.setActionTime(new Date());
            history.setRemark("流程启动");
            workflowHistoryService.save(history);
        }
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean approveTask(Long taskId, Long userId, String action, String remark) {
        WorkflowTask task = getById(taskId);
        if (task == null || task.getStatus() != 0) {
            return false;
        }
        
        // 更新任务状态
        task.setStatus(1); // 已办
        task.setEndTime(new Date());
        boolean result = updateById(task);
        
        // 记录历史
        if (result) {
            WorkflowHistory history = new WorkflowHistory();
            history.setTaskId(taskId);
            history.setAction(action);
            history.setUserId(userId);
            history.setActionTime(new Date());
            history.setRemark(remark);
            workflowHistoryService.save(history);
        }
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean revokeTask(Long taskId, Long userId, String remark) {
        WorkflowTask task = getById(taskId);
        if (task == null || task.getStatus() != 0) {
            return false;
        }
        
        // 更新任务状态
        task.setStatus(3); // 撤回
        task.setEndTime(new Date());
        boolean result = updateById(task);
        
        // 记录历史
        if (result) {
            WorkflowHistory history = new WorkflowHistory();
            history.setTaskId(taskId);
            history.setAction("撤回");
            history.setUserId(userId);
            history.setActionTime(new Date());
            history.setRemark(remark);
            workflowHistoryService.save(history);
        }
        
        return result;
    }
} 